package club.hicode.spring.factory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 抽象的 BeanFactory,完成部分细节操作
 *
 * @author Liu Chunfu
 * @date 2018-04-26 下午3:47
 **/
public abstract class AbstractBeanFactory implements BeanFacotry {

    /**
     * bean 容器
     */
    private Map<String, BeanDefinition> container = new ConcurrentHashMap<>();


    @Override
    public void registerBean(String beanName, BeanDefinition beanDefinition) {
        Object bean = createBean(beanDefinition);
        beanDefinition.setObject(bean);
        //beanDefinition.setBeanName(beanName);
        container.put(beanName, beanDefinition);
    }

    @Override
    public Object getBean(String beanName) {
        return container.get(beanName).getObject();
    }

    /**
     * 如何创建交给子类去实现
     *
     * @return bean
     */
    public abstract Object createBean(BeanDefinition beanDefinition);

}
